package com.woniuxy.ticket.ticketmovie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.ticket.ticketmovie.entity.Type;
import com.woniuxy.ticket.ticketmovie.exception.TypeNameExistException;
import com.woniuxy.ticket.ticketmovie.mapper.TypeMapper;
import com.woniuxy.ticket.ticketmovie.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class TypeServiceImpl extends ServiceImpl<TypeMapper, Type> implements TypeService {

    @Autowired
    private TypeMapper typeMapper;

    @Transactional
    @Override
    public void add(Type type) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("name",type.getName());
        if(typeMapper.selectCount(queryWrapper) == 0){
            typeMapper.insert(type);
        }else {
            throw new TypeNameExistException();
        }
    }

    @Transactional
    @Override
    public void updateType(Type type) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("name",type.getName());
        if(typeMapper.selectOne(queryWrapper).getId() != type.getId()){
            typeMapper.updateById(type);
        }else {
            throw new TypeNameExistException();
        }
    }


}
